/*****************************************************************************/
/*                                                                           */
/*    Domino Operation System Sound Module                                   */
/*                                                                           */
/*    Copyright (C) 2013 Laszlo Arvai                                        */
/*    All rights reserved.                                                   */
/*                                                                           */
/*    This software may be modified and distributed under the terms          */
/*    of the BSD license.  See the LICENSE file for details.                 */
/*                                                                           */
/*    ------------------------------------------------------------------   */
/*    Multi channel wave output mixer                                        */
/*****************************************************************************/

///////////////////////////////////////////////////////////////////////////////
// Includes
#include <sndWave.h>
#include "drvIOConfig.h"

///////////////////////////////////////////////////////////////////////////////
// Constants
#ifndef sndWAVE_CHANNEL_COUNT
#define sndWAVE_CHANNEL_COUNT 16
#endif

///////////////////////////////////////////////////////////////////////////////
// Types
typedef struct
{
	dosROMAddress Handle;						//! Handle of the resource wave data to be played
	dosWaveSamplePointer Length;		//! Length of the wave data (in samples)
	dosWaveSamplePointer Position;	//! Current playback position
	dosBool Looped;									//! true if playback is looped
} WaveChannelInformation;

///////////////////////////////////////////////////////////////////////////////
// Module local variables
static volatile dosHandle l_position = 0;
static volatile dosWord l_remaining = 0;
static volatile dosByte l_divisor = 0;
static dosROMAddress l_wave_resource_address = 0;
//static WaveChannelInformation l_channel[sndWAVE_CHANNEL_COUNT];

///////////////////////////////////////////////////////////////////////////////
// Initialize wave device
void sndInitWaveDevice(void)
{
	// init wave resource address
#ifdef PAR_ADDRESS_ABSOLUTE
	l_wave_resource_address = 0;
#else
	l_wave_resource_address = krnlGetResourceAddress( 0x45564157ul );
#endif
}

///////////////////////////////////////////////////////////////////////////////
// Play wave
dosBool sndPlayWave( dosHandle in_wave_handle )
{
	dosROMAddress address;

	// get resource data

#ifndef PAR_ADDRESS_ABSOLUTE
	// if no wave chunk in the resource file
	if( l_wave_resource_address == 0 )
		return dosFalse;
#endif

#ifdef PAR_ADDRESS_ABSOLUTE
	address = in_wave_handle;
#else
	address = l_wave_resource_address + in_wave_handle;
#endif

	// check format
	if( dosROMReadByte( address++ ) != 1 )
		return dosFalse;

	// load size
	l_remaining = (dosWord)(dosROMReadDWord( address ) );

	// address of wave data
	l_position = address + sizeof( dosDWord );

	l_divisor = 0;



	return dosTrue;
}


dosBool sndPlayWaveLoop( dosHandle in_wave_handle )
{
	return dosTrue;
}

dosBool sndStopWave(dosHandle in_wave_handle)
{
}

///////////////////////////////////////////////////////////////////////////////
// Is Wave Out Busy
dosBool sndIsWaveOutBusy(void)
{
	return (l_remaining > 0);
}

///////////////////////////////////////////////////////////////////////////////
// Stop Wave Out
void sndStopWaveOut(void)
{
	l_remaining = 0;


}
